#include<iostream>
int findmin(int* ret, int* m)
{
    int num = ret[0], col = *m;
    *m = 0;
    while (col--)
    {
        if (ret[col] < num)
        {
            num = ret[col];
            *m = col;
        }
    }
    return num;
}
int is_colmax(int** ret, int rowmin, int row, int col)
{
    while (row--)
    {
        if (ret[row][col] > rowmin)
            return 0;
    }
    return 1;
}
int* luckyNumbers(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
    int* ret = (int*)malloc(0);
    int k = 0;
    for (int i = 0; i < matrixSize; i++)
    {
        int col = *matrixColSize;
        int rowmin = findmin(matrix[i], &col);
        if (is_colmax(matrix, rowmin, matrixSize, col))
        {
            ret = (int*)realloc(ret, sizeof(int) * ++k);
            ret[k - 1] = rowmin;
            *returnSize = k;
        }
    }
    if (k == 0)
    {
        *returnSize = 0;
        return NULL;
    }
    return ret;
}